#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int BTDataType;
typedef struct BinaryTreeNode
{
	BTDataType data;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;

}BTNode;



BTNode* BuyNode(int x)
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	if (node == NULL)
	{
		perror("malloc fail");
		return NULL;
	}

	node->data = x;
	node->left = NULL;
	node->right = NULL;
	return node;
}



int TreeKlevel(BTNode* root,int k)
{
	if (root == NULL)
		return 0;

	if (k == 1)
		return 1;

	int lNode=TreeKlevel(root->left, k - 1);
	int rNode=TreeKlevel(root->right, k - 1);
	return lNode + rNode;
}



BTNode* TreeFind(BTNode* root, int x)
{
	if (root == NULL)
		return NULL;

	if (root->data == x)
		return root;

	BTNode* lNode = TreeFind(root->left, x);
	if (lNode)
		return lNode;
	BTNode* rNode = TreeFind(root->right, x);
	if (rNode)
		return rNode;

	return NULL;
}
int main()
{
	BTNode* n1 = BuyNode(1);
	BTNode* n2 = BuyNode(2);
	BTNode* n3 = BuyNode(3);
	BTNode* n4 = BuyNode(4);
	BTNode* n5 = BuyNode(5);
	BTNode* n6 = BuyNode(6);
	
	n1->left = n2;
	n1->right = n4;
	n2->left = n3;
	n4->left = n5;
	n4->right = n6;

	printf("Treeklevel:%d", TreeKlevel(n1,3));
	printf("\n");
	printf("TreeFind: %p", TreeFind(n1, 5));
	printf("\n");

	printf("TreeFind: %p", TreeFind(n1, 50));



	return 0;
}